/* eslint-disable default-param-last */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable react/no-unstable-nested-components */
/* eslint-disable no-shadow */
/* eslint-disable no-debugger */
/* eslint-disable react/no-array-index-key */

/**
 * 表单主体
 * @Author Lianqi
 * @Date 2022-3-22
 * @description V0.2 增加功能：1、表单布局，2、表单字段配置式，3、表单对外提供事件；
 */

 import React, { useImperativeHandle, useState } from 'react'
 import { Form } from 'antd'
 import FormLayout from './FormLayout'
 import { extendInitialValues, extendChangedValues } from './data/FormDataParser'
 import { MODE, LAYOUT, LABEL_COL, WRAPPER_COL, ALIGN } from '../config/Constant'
 
 function FormMain(props) {
   const {
     mode = MODE.EDIT,
     name: tableName,
     initialValues = {},
     layoutProps,
     // fields
     fields,
     // ref
     realFormRef,
     // events
     onFinish = () => {},
     onFinishFailed = () => {},
     onValuesChange = () => {},
     onInnerReset,
     // rest
     ...restProps
   } = props
   const {
     layout = LAYOUT.HORIZONAL,
     labelCol = LABEL_COL,
     wrapperCol = WRAPPER_COL,
     labelAlign = ALIGN.RIGHT,
     labelWrap = false,
     colon = true,
   } = layoutProps || {}
 
   // 表单Props
   const formProps = {
     layout: layout === LAYOUT.GRID ? LAYOUT.HORIZONAL : layout,
     labelCol,
     wrapperCol,
     disabled: mode === MODE.DISABLED,
   }
   const labelProps = {
     labelAlign,
     labelWrap,
     colon,
   }
 
   // 表单实例引用、数据处理
   const [form] = Form.useForm()
   const initValues = extendInitialValues(initialValues, fields)
 
   // events
   const handleFinish = (values) => {
     if (onFinish) {
       const ex = extendChangedValues(values, fields)
       onFinish(ex.values)
       // debugger
     }
   }
 
   const handleFinishFailed = (values, errorFields, outOfDate) => {
     if (onFinishFailed) {
       const ex = extendChangedValues(values, fields)
       onFinishFailed(ex.values, errorFields, outOfDate)
     }
   }
 
   const handleValuesChange = (changedValues, allValues) => {
     // 处理联动字段
     const ex = extendChangedValues(allValues, fields)
     if (Object.keys(ex.linkageValues).length > 0) {
       form.setFieldsValue({ ...ex.linkageValues })
     }
 
     // emit change
     onValuesChange(ex.values, allValues)
   }
 
   const handleReset = () => {
     onInnerReset()
   }
 
   // 暴露给外部组件的方法
   useImperativeHandle(realFormRef, () => ({
     getForm: () => form,
     getFieldError: (fieldName) => form.getFieldError(fieldName),
     // getFieldInstance: (fieldName) => form.getFieldInstance(fieldName),
     getFieldsError: (nameList) => form.getFieldsError(nameList),
     getFieldsValue: () => form.getFieldsValue(true),
     getFieldValue: (fieldName) => form.getFieldValue(fieldName),
     resetFields: (fields) => {
       if (fields) {
         form.resetFields(fields)
       } else {
         form.resetFields()
       }
     },
     setFields: (fields) => {
       // debugger
       form.setFields(fields)
     },
     setFieldValue: (name, value) => form.setFieldValue(name, value),
     setFieldsValue: (values) => {
       const newValues = extendInitialValues(values, fields)
       form.setFieldsValue({ ...newValues })
     },
     submit: () => {
       form.submit()
     },
    //  validateFields: (nameList) => form.validateFields(nameList),
     validateFields: (nameList) => {
      return form.validateFields(nameList).then(params => {
        const ex = extendChangedValues(params, fields)
        return ex.values
      })
     },
   }))
 
   const getWindowSize = () => ({
     innerHeight: window.innerHeight,
     innerWidth: window.innerWidth,
   });
 
   const [windowSize] = useState(getWindowSize());
 
   return (
     <Form
       name={tableName}
       form={form}
       onFinish={handleFinish}
       onFinishFailed={handleFinishFailed}
       onValuesChange={handleValuesChange}
       initialValues={{ ...initValues }}
       disabled={formProps.disabled}
       {...formProps}
       {...labelProps}
      //  size={windowSize.innerWidth < 1600 ? 'small' : 'default'}
     >
       {/* 当分辨率小于1600，将表格size=small */}
       <FormLayout
         mode={mode}
         name={tableName}
         fields={fields}
         initialValues={{ ...initValues }}
         layoutProps={layoutProps}
         labelProps={labelProps}
         handleReset={handleReset}
         {...restProps}
       />
     </Form>
   )
 }
 
 export default FormMain
 